(ns forum.core-test
  (:use clojure.test
        forum.core
        forum.models
        clj-webdriver.taxi))

(defn admin-login[]
  (quick-fill-submit {"#username" "admin"}
                   {"#password" "admin"}
                   {"#password" submit}))

(deftest login-test
  (is (= "http://localhost:8080/" (current-url)))
  (admin-login)
  (is (= true (exists? (find-element {:tag :td, :text "Category"}))))
  (click "a[href*='logout']"))

(deftest logout-test
  (admin-login)
  (click "a[href*='logout']")
  (is (= "http://localhost:8080/" (current-url))))

(deftest create-category-test
  (admin-login)
  (click "a[href*='category']")
  (is (= "http://localhost:8080/category" (current-url)))
  (quick-fill-submit {"#title" "Test"}
                   {"#description" "This is test category"}
                   {"#description" submit})
  (is (= true (exists? (find-element {:text "Test"}))))
  (delete-category-by-title "Test")
  (click "a[href*='logout']"))

(deftest view-categories-test
  (admin-login)
  (is (= true (exists? (find-element {:tag :td, :text "Category"}))))
  (is (= true (exists? (find-element {:tag :td, :text "Description"}))))
  (is (= true (exists? (find-element {:text "first category"}))))
  (is (= true (exists? (find-element {:text "This is the first category created on this forum"}))))
  (click "a[href*='logout']"))

(deftest view-topics-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (is (= true (exists? (find-element {:tag :td, :text "Title of the Topic"}))))
  (is (= true (exists? (find-element {:tag :td, :text "Created by"}))))
  (is (= true (exists? (find-element {:text "first topic"}))))
  (is (= true (exists? (find-element {:text "admin"}))))
  (click "a[href*='logout']"))

(deftest create-topic-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click "a[href*='thread']")
  (quick-fill-submit {"#title" "Test topic"}
                   {"#content" "This is test"}
                   {"#content" submit})
  (is (= true (exists? (find-element {:text "Test topic"}))))
  (click (find-element {:text "Test topic"}))
  (is (= true (exists? (find-element {:text "            This is test"}))))
  (click "a[href*='logout']")
  (delete-post-by-content "            This is test")
  (delete-topic-by-title "Test topic"))

(deftest view-posts-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click (find-element {:text "first topic"}))
  (is (= true (exists? (find-element {:tag :td, :text "Author"}))))
  (is (= true (exists? (find-element {:tag :td, :text "Post content"}))))
  (is (= true (exists? (find-element {:text "admin"}))))
  (click "a[href*='logout']"))

(deftest add-post-to-topic-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click (find-element {:text "first topic"}))
  (click "a[href*='addpost']")
  (quick-fill-submit {"#content" "This is test"}
                   {"#content" submit})
 
  (is (= true (exists? (find-element {:text "            This is test"}))))
  (click "a[href*='logout']")
  (delete-post-by-content "            This is test"))

(deftest register-user-test
  (click "a[href*='register']")
  (quick-fill-submit {"#username" "test"}
                   {"#password" "test"}
                   {"#email" "test"}
                   {"#email" submit})
  (quick-fill-submit {"#username" "test"}
                   {"#password" "test"}
                   {"#password" submit})
  (click "a[href*='logout']")
  (delete-user-by-username "test"))

(deftest view-users-test
  (admin-login)
  (click "a[href*='viewusers']")
  (is (= true (exists? (find-element {:tag :td, :text "Username"}))))
  (is (= true (exists? (find-element {:tag :td, :text "Password"}))))
  (is (= true (exists? (find-element {:tag :td, :text "e-mail"}))))
  (is (= true (exists? (find-element {:tag :td, :text "Role"}))))
  (is (= true (exists? (find-element {:text "admin"}))))
  (click "a[href*='logout']"))

(deftest view-profile-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click (find-element {:text "first topic"}))
  (click (find-element {:text "admin"}))
  (is (= true (exists? (find-element {:tag :td, :text "Username"}))))
  (is (= true (exists? (find-element {:tag :td, :text "e-mail"}))))
  (is (= true (exists? (find-element {:tag :td, :text "Role"}))))
  (is (= true (exists? (find-element {:text "admin"}))))
  (click "a[href*='logout']"))


(deftest edit-user-test
  (click "a[href*='register']")
  (quick-fill-submit {"#username" "test"}
                   {"#password" "test"}
                   {"#email" "test@gmail.com"}
                   {"#email" submit})
  (admin-login)
  (click "a[href*='viewusers']")
  (click (find-element {:text "Edit test"}))
  (quick-fill-submit {"#username" "2"}
                   {"#password" "2"}
                   {"#email" "2"}
                   {"#email" submit})
  (is (= true (exists? (find-element {:text "test2"}))))
  (delete-user-by-username "test2")
  (click "a[href*='logout']"))

(deftest delete-post-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click (find-element {:text "first topic"}))
  (click "a[href*='addpost']")
  (quick-fill-submit {"#content" "This is test"}
                   {"#content" submit})
 
  (is (= true (exists? (find-element {:text "            This is test"}))))
  (click (str  "a[href*='/deletepost/" (dec (generate-posts-id))  "']"))
  (is (= false (exists? (find-element {:text "            This is test"}))))
  (click "a[href*='logout']"))

(deftest edit-post-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click (find-element {:text "first topic"}))
  (click "a[href*='addpost']")
  
  (quick-fill-submit {"#content" "This is test"}
                   {"#content" submit})
 
  (is (= true (exists? (find-element {:text "            This is test"}))))
  (click (str  "a[href*='/editp/" (dec (generate-posts-id))  "']"))
  (quick-fill-submit {"#content" "2"}
                   {"#content" submit})
  (click (find-element {:text "first category"}))
  (click (find-element {:text "first topic"}))
  (is (= true (exists? (find-element {:text "            This is test2"}))))
  (delete-post-by-content "            This is test2")
  (click "a[href*='logout']"))

(deftest edit-topic-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click "a[href*='thread']")
  (quick-fill-submit {"#title" "Test topic"}
                   {"#content" "This is test"}
                   {"#content" submit})
  (is (= true (exists? (find-element {:text "Test topic"}))))
  (click (find-element {:text "Edit Test topic"}))
  (quick-fill-submit {"#title" "2"}
                   {"#title" submit})
  (click (find-element {:text "first category"}))
  (is (= true (exists? (find-element {:text "Test topic2"}))))
  (delete-post-by-content "            This is test")
  (delete-topic-by-title "Test topic2")
  (click "a[href*='logout']"))

(deftest delete-topic-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click "a[href*='thread']")
  (quick-fill-submit {"#title" "Test topic"}
                   {"#content" "This is test"}
                   {"#content" submit})
  (is (= true (exists? (find-element {:text "Test topic"}))))
  (click (find-element {:text "Delete Test topic"}))
  (click (find-element {:text "first category"}))
  (is (= false (exists? (find-element {:text "Test topic"}))))
  (click "a[href*='logout']"))

(deftest view-news-test
  (admin-login)
  (click (find-element {:text "View News"}))
  (is (= true (exists? (find-element {:text "admin"}))))
  (is (= true (exists? (find-element {:text "News"}))))
  (click "a[href*='logout']"))

(deftest add-news-test
  (admin-login)
  (click (find-element {:text "View News"}))
  (click (find-element {:text "Add News"}))
  (quick-fill-submit {"#title" "Test news"}
                   {"#content" "This is test"}
                   {"#content" submit})
  (is (= true (exists? (find-element {:text "admin"}))))
  (is (= true (exists? (find-element {:text "Test news"}))))
  (delete-news-by-title "Test news")
  (click "a[href*='logout']"))

(deftest add-reply-test
  (admin-login)
  (click (find-element {:text "first category"}))
  (click "a[href*='thread']")
  (quick-fill-submit {"#title" "Test topic"}
                   {"#content" "This is test"}
                   {"#content" submit})
  (is (= true (exists? (find-element {:text "Test topic"}))))
  (click (find-element {:text "Test topic"}))
  (click (find-element {:text "Reply to post"}))
  (quick-fill-submit {"#content" "Quote test"}
                   {"#content" submit})
  (is (= true (exists? (find-element {:tag :div, :text "            This is test"}))))
  (is (= true (exists? (find-element {:text "            Quote test"}))))
  (click (find-element {:text "Go to Index"}))
  (click (find-element {:text "first category"}))
  (click (find-element {:text "Delete Test topic"}))
  (click "a[href*='logout']"))

(deftest inbox-test
  (admin-login)
  (click (find-element {:text "Private Messages"}))
  (is (= true (exists? (find-element {:text "Sender"}))))
  (is (= true (exists? (find-element {:text "Message title"}))))
  (is (= true (exists? (find-element {:text "Re:hello"}))))
  (is (= true (exists? (find-element {:text "vesko"}))))
  (click "a[href*='logout']"))

(deftest outbox-test
  (quick-fill-submit {"#username" "vesko"}
                   {"#password" "vesko"}
                   {"#password" submit})
  (click (find-element {:text "Private Messages"}))
  (click (find-element {:text "Outbox"}))
  (is (= true (exists? (find-element {:text "Receiver"}))))
  (is (= true (exists? (find-element {:text "Message title"}))))
  (is (= true (exists? (find-element {:text "Re:hello"}))))
  (is (= true (exists? (find-element {:text "admin"}))))
  (click "a[href*='logout']"))


(deftest inbox-message-test
  (admin-login)
  (click (find-element {:text "Private Messages"}))
  (click (find-element {:text "Re:hello"}))
  (is (= true (exists? (find-element {:tag :div, :text "hello there"}))))
  (is (= true (exists? (find-element {:text "hi"}))))
  (click "a[href*='logout']"))


(deftest search-test
  (admin-login)
  (click (find-element {:text "Search"}))
  (quick-fill-submit {"#key" "first blabla2"}
                   {"#key" submit})
  
  (is (= true (exists? (find-element {:text "first"}))))
  (is (= true (exists? (find-element {:text "blabla2"}))))
  (click "a[href*='logout']"))


(defn set-up-fixture [f]
  (set-driver! {:browser :firefox} "http://localhost:8080")
  (f)
  (quit))

(use-fixtures :once set-up-fixture)